import { createRouter, createWebHistory, RouteRecordRaw } from 'vue-router'
import NProgress from 'nprogress' // 引入一份进度条插件
import 'nprogress/nprogress.css' // 引入进度条样式
import Home from '../views/home/Index.vue'
//权限管理
import Authority from "../views/authority/Index.vue";
// 首页
import System from "../views/system/Index.vue";
//考勤
import Attendance from "../views/attendance/Index.vue"
// 审批
import Approve from "../views/approve/Index.vue";
// 公司设置
import Company from "../views/company/Index.vue";
// 公司信息
import CompanyInfo from "../views/company/Info.vue";
// 角色管理
import CompanyRole from "../views/company/Role.vue";
// 组织架构
import Organization from "../views/organization/Index.vue";
// 工资
import Salary from "../views/salary/Index.vue";
// 社保
import Security from "../views/security/Index.vue";
// 员工
import Staff from "../views/staff/Index.vue";
import Login from "../views/login/Index.vue";
import { ElMessage } from "element-plus";
import { el } from 'element-plus/es/locale';
import { useStore } from 'vuex';

const routes: Array<any> = [
  {
    path: '/',
    name: 'home',
    component: Home,
    redirect: '/system',
    children: [
      {
        path: '/system',
        name: 'system',
        component: System,
        meta: {
          title: "首页",
          key: '/system'
        }
      },
      {
        path: '/staff',
        name: 'staff',
        component: Staff,
        meta: {
          title: "员工",
          key: '/staff'
        }
      }, {
        path: '/company',
        name: 'company',
        component: Company,
        meta: {
          title: "公司设置",
          key: '/company'
        }
      }, {
        path: '/authority',
        name: 'authority',
        component: Authority,
        meta: {
          title: "权限管理",
          key: '/authority'
        },
      }, {
        path: '/security',
        name: 'security',
        component: Security,
        meta: {
          title: "社保",
          key: '/security'
        }
      }, {
        path: '/approve',
        name: 'approve',
        component: Approve,
        meta: {
          title: "审批",
          key: '/approve'
        }
      }, {
        path: '/attendance',
        name: 'attendance',
        component: Attendance,
        meta: {
          title: "考勤",
          key: '/attendance'
        }
      }, {
        path: '/salary',
        name: 'salary',
        component: Salary,
        meta: {
          title: "工资",
          key: '/salary'
        }
      }, {
        path: '/organization',
        name: 'organization',
        component: Organization,
        meta: {
          title: "组织架构",
          key: '/organization'
        }
      },
    ]
  }
  , {
    path: '/login',
    name: 'login',
    component: Login
  }
]
// 定义白名单  所有不受权限控制的页面
const whiteList = ['/login', '/404']
const router = createRouter({
  routes,
  // 禁止浏览器 前进和后退，另一部分在main.ts中
  // scrollBehavior: () => {
  //   history.pushState(null, document.URL)
  // },
  history: createWebHistory(process.env.BASE_URL),
  // routes
})
//登录拦截 - 前置路由守卫
router.beforeEach((to, from, next) => {
  // 开启进度条
  NProgress.start()
  //  首先判断有无token
  if (localStorage.getItem('token')) {
    //  如果有token 判断是不是去登录页
    if (to.path === '/login') {
      // 跳到主页
      next('/')
      // 手动强制关闭一次  为了解决 手动切换地址时  进度条的不关闭的问题
      NProgress.done()
    } else {
      next()
    }
    // if (to.path != '/login') {
    //   if (!localStorage.getItem('token')) {
    //     ElMessage({
    //       type: 'warning',
    //       message: '请先登陆！',
    //     })
    //     next('/login')
    //     return
    //   }
    // }
    // next()
  } else {
    // 如果没有token
    if (whiteList.indexOf(to.path) > -1) {
      // 如果找到了 表示在在名单里面
      next()
    } else {
      // 跳到登录页
      next('/login')
      // 手动强制关闭一次  为了解决 手动切换地址时  进度条的不关闭的问题
      NProgress.done()
    }
  }
})
// 后置守卫
router.afterEach(() => {
  NProgress.done() // 关闭进度条
})
export default router
